package com.gitee.tanmingjian.httptunnel.core.bean;

import com.gitee.tanmingjian.httptunnel.core.util.Base64Utils;
import com.gitee.tanmingjian.httptunnel.core.util.StringUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class QueryParam {

    private static final String DEFAULT_LOGIN = "root";
    private static final String ENABLE_BASE64_FLAG = "1";
    private String host;
    private String port;
    private String login;
    private String password;
    private String db;
    private String actn;
    private List<String> q;

    private String encodeBase64;

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getLogin() {
        return StringUtils.isBlank(login) ? DEFAULT_LOGIN : login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getDb() {
        return db;
    }

    public void setDb(String db) {
        this.db = db;
    }

    public String getActn() {
        return actn;
    }

    public void setActn(String actn) {
        this.actn = actn;
    }

    public List<String> getQ() {
        if (StringUtils.equals(getEncodeBase64(), ENABLE_BASE64_FLAG)) {
            return q.stream().map(o -> new String(Base64Utils.decodeBase64(o))).collect(Collectors.toList());
        }
        return q;
    }

    public void setQ(List<String> q) {
        this.q = q;
    }

    public String getEncodeBase64() {
        return encodeBase64;
    }

    public void setEncodeBase64(String encodeBase64) {
        this.encodeBase64 = encodeBase64;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        QueryParam that = (QueryParam) o;
        return Objects.equals(host, that.host) && Objects.equals(port, that.port) && Objects.equals(db, that.db) && Objects.equals(login, that.login) && Objects.equals(password, that.password);
    }

    @Override
    public int hashCode() {
        return Objects.hash(host, port, db, login, password);
    }

    @Override
    public String toString() {
        return "QueryParam{" +
                "host='" + host + '\'' +
                ", port='" + port + '\'' +
                ", login='" + login + '\'' +
                ", password='" + password + '\'' +
                ", db='" + db + '\'' +
                ", actn='" + actn + '\'' +
                ", q=" + q +
                ", encodeBase64='" + encodeBase64 + '\'' +
                '}';
    }

    public String getSessionFlag(String sessionId) {
        if (sessionId == null) {
            throw new RuntimeException("sessionId不能为空");
        }
        return String.format("%s_%s@%s:%s_%s", sessionId, login, host, port, StringUtils.getMD5String(password));
    }
}
